package jp.co.sharp.android.parents.kidsguard.activity.edit.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import jp.co.sharp.android.parents.kidsguard.R;

public class SelfInfoNoteDlg extends Dialog {

    private OnNoteSetListener mOnNoteSetListener;

    private SelfInfoNoteDlg mSelfInfoNoteDlg;

    private String mNote;

    public SelfInfoNoteDlg(@NonNull Context context) {
        super(context);
        mSelfInfoNoteDlg=this;
    }

    public SelfInfoNoteDlg(@NonNull Context context, int themeResId, String note) {
        super(context, themeResId);
        mSelfInfoNoteDlg=this;
        this.mNote=note;

    }

    protected SelfInfoNoteDlg(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        mSelfInfoNoteDlg=this;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.self_info_note_dlg);
        EditText editText =(EditText)this.findViewById(R.id.editViewMain);
        editText.setText(mNote);

        TextView editView = findViewById(R.id.editViewMain);
        editView.setMovementMethod(ScrollingMovementMethod.getInstance());
//        EditText clickEditText = (EditText)v;
//        String text =clickEditText.getText().toString().trim();

        editView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                EditText clickEditText = (EditText)v;
//                note =clickEditText.getText().toString().trim();
            }
        });
        // EditText焦点的监听
        editView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {

            }
        });
        // 对EditText内容的实时监听
        editView.addTextChangedListener(new TextWatcher() {
            // 输入文本之前的状态
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//                System.out.println("s"+s.toString());
            }
            // 输入文本中的状态
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }
            // 输入文本之后的状态
            @Override
            public void afterTextChanged(Editable s) {
                mNote=s.toString();
            }
        });

        Button buttonOK = (Button)this.findViewById(R.id.yes);
        buttonOK.setOnClickListener(new SelfInfoNoteDlg.ListenerOK());

        Button buttonCancel = (Button)this.findViewById(R.id.cancel);
        buttonCancel.setOnClickListener(new SelfInfoNoteDlg.ListenerCancel());

    }

    /**
     *
     */
    public interface OnNoteSetListener {
        void onNoteSet(String note);
    }

    /*
     * 对外公开方法让Activity实现
     */
    public void setOnNoteSetListener(OnNoteSetListener callBack) {
        mOnNoteSetListener = callBack;
    }

    class ListenerOK implements View.OnClickListener {

        @Override
        public void onClick(View view) {
            if (mOnNoteSetListener != null) {
                mOnNoteSetListener.onNoteSet(mNote);
                mSelfInfoNoteDlg.dismiss();
            }
        }
    }

    class ListenerCancel implements View.OnClickListener {

        @Override
        public void onClick(View view) {
            if (mOnNoteSetListener != null) {
                mSelfInfoNoteDlg.dismiss();
            }
        }
    }




}
